'use strict';

const Controller = require('../../../core/common');

class UsersController extends Controller {

  async index() {
    const { ctx } = this;

    const { type, nickname, openId } = ctx.request.query;

    const attributes = ['id', 'openId', 'nickname', 'totalIn', 'amount', 'createdAt', 'updatedAt'];
    const where = {};
    type && Object.assign(where, { type });
    nickname && Object.assign(where, { nickname: { $like: `%${nickname}%` } });
    openId && Object.assign(where, { openId });

    const data = await ctx.service.v1.users.findAll({ attributes, where });

    this.success(data);
  }

  async destroy() {
    const { ctx } = this;

    const { id = '' } = ctx.params;

    const data = await ctx.service.v1.users.destroy({
      where: {
        id: id.indexOf(',') === -1 ? id : { '$in': id.split(',') }
      }
    });

    this.success(data);
  }

}

module.exports = UsersController;